package com.gjl.mgmt.view.web.action;

import java.io.PrintWriter;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.gjl.app.model.business.blog.entity.BlogCategoryEntity;
import com.gjl.app.view.web.BaseAction;
import com.google.appengine.repackaged.com.google.common.base.StringUtil;

public class BlogCategoryMgmtAction extends BaseAction{

	private static final long serialVersionUID = -7367248725103592437L;
	
	private String blogCategoryName;
	
	public String getBlogCategoryName() {
		return blogCategoryName;
	}

	public void setBlogCategoryName(String blogCategoryName) {
		this.blogCategoryName = blogCategoryName;
	}

	@Override
	public String execute() throws Exception {
		try{
			EntityManager entityManager = getEntityManager();
			String cateQueryStr = "SELECT cate FROM " + BlogCategoryEntity.class.getName() + " cate";
			Query cateQuery = entityManager.createQuery(cateQueryStr);
			
			@SuppressWarnings("unchecked")
			List<BlogCategoryEntity> result = (List<BlogCategoryEntity>)cateQuery.getResultList();
			if (result != null && result.size() > 0) {
				HttpServletRequest request = ServletActionContext.getRequest();
				request.setAttribute("result", result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.execute();
	}
	
	public String newCategory() throws Exception {
		boolean flag = false;
		Long id = null;
		try{
			if(blogCategoryName == null || StringUtil.isEmptyOrWhitespace(blogCategoryName)) {
				this.addFieldError("", "Could not be null");
			}
			EntityManager entityManager = getEntityManager();
			BlogCategoryEntity entity = new BlogCategoryEntity();
			entity.setCategory(blogCategoryName);
			EntityTransaction utx = entityManager.getTransaction();
			utx.begin();
			String query = "SELECT cate FROM " + BlogCategoryEntity.class.getName() + " cate WHERE cate.category = :name";
			Query cateQuery = entityManager.createQuery(query);
			cateQuery.setParameter("name", blogCategoryName);
			
			if (cateQuery.getResultList() == null || cateQuery.getResultList().size() < 1) {
				entityManager.persist(entity);
				entityManager.flush();
				id = entity.getId();
			}
			utx.commit();
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		
		
//		String responseContent = generateJsonResponse(flag, id);
//		response.setContentType("application/json;charset=UTF-8");
//		response.setContentLength(responseContent.length());
		PrintWriter writer = response.getWriter();
		writer.write(generateJsonResponse(flag, id));
		writer.flush();
		return null;
	}
	
	private String generateJsonResponse(boolean flag, Long id) {
		StringBuffer responseBuffer = new StringBuffer();
		responseBuffer.append("{\"flag\":").append(flag);
		if (id == null) {
			responseBuffer.append(",\"id\":null}");
		} else {
			responseBuffer.append(",\"id\":").append(id);
			responseBuffer.append("}");
		}
		return responseBuffer.toString();
	}

}
